package utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

/**
 * 日志-字段变化-工具类
 */
public class LogUtils {
    //beanModifyMsg
    public static String BeanModifyMsg(Object newObject, Map<String, Object> oldMap){
        try {
            Map<String, Object> newMap = ObjectToMapUtils.objectToMap(newObject);
            StringBuilder str = new StringBuilder();
            oldMap.forEach((k, v) -> {
                Object newResult = newMap.get(k);
                if (v == null){
                    if (v instanceof Date) {
                        newResult = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)newResult);
//                        newResult = format;
                        if(v != null){
                            v = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)v);
                        }
                    }
                    if (newResult instanceof Date){
                        newResult = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)newResult);
                    }
                    if (newResult != null) {
                        str.append("将【").append(k + "】").append("从[").append(v).append("]").append("设置为[").append(newResult).append("];\n");
                    }
                }
                if (null != v && newResult == null){
                    if (v instanceof Date) {
                        String sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)v);
                        v = sdf;
                        if (newResult != null) {
                            newResult = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)newResult);
                        }
                    }
                    str.append("将【").append(k + "】").append("从[").append(v).append("]").append("设置为[").append(newResult).append("];\n");
                }
                if (null!=v && newResult != null && !v.equals(newResult)) {
                    if (v instanceof Date) {
                        System.out.println("--"+v);
                        String sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)v);
                        v = sdf;
                        if (newResult != null) {
                            newResult = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date)newResult);
//                            newResult = format;
                        }
                    }
                    if (newResult != null) {
                        str.append("将【").append(k + "】").append("从[").append(v).append("]").append("设置为[").append(newResult).append("];\n");
                    }
                }
            });
            return str.toString();
        } catch (Exception e) {
            throw new RuntimeException("比较异常",e);
        }
    }

    public static String MapToStringMethod(Map map){
        if (map.isEmpty()){
            return "";
        }
        String content = "";
        Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Integer> entry = it.next();
            content += "将【"+entry.getKey() +"】设置为[" + entry.getValue()+"];\n";
        }
        return content;
    }
}
